import {HTTP} from "../until/http-p.js";
 class KeywordModel extends HTTP{
   key="q"
   maxLength=10
  getHistory(){  //从缓存中获取历史
    let words = wx.getStorageSync(this.key);
    if(!words)
    {
      return [];
    }
    return words;
  }
  getHot(){  //获取热门搜索
    return this.request({
      url:"/book/hot_keyword"
    })
  }
  addToHistory(keyword){ //向缓存添加记录
    let words=this.getHistory();
    const has=words.includes(keyword); //includes判断数组中有没有该值
    if(!has)  //如果没有的话  就加入缓存
    {
      //限制数量 先把末尾的删除再把新的放到首位
      const length=words.length;
      if(length>this.maxLength)
      {
        words.pop();
      }
      words.unshift(keyword);
      wx.setStorageSync(this.key, words);
    }
  }
 }
 export {KeywordModel}